package com.hefei.customer.info.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hefei.common.returncode.ReturnCode;
import com.hefei.customer.info.dao.ICustomerDao;
import com.hefei.customer.info.po.Contact;
import com.hefei.customer.info.po.Customer;
import com.hefei.customer.info.service.ICustomerService;
import com.hefei.service.framework.base.request.RequestThreadLocal;
import com.hefei.service.framework.exception.DaoException;
import com.hefei.service.framework.exception.ServiceException;

@Service
public class CustomerService implements ICustomerService {
	
	private Logger logger = Logger.getLogger(CustomerService.class);
	@Autowired
	private ICustomerDao customerDao;

	@Override
	public void saveCustomerInfo(Customer customer) throws ServiceException {
		if(customer == null){
			throw new ServiceException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			Date now = Calendar.getInstance().getTime();
			customer.setUpdateTime(now);
			customer.setCreateTime(now);
			customerDao.save(customer);
		} catch (DaoException e) {
			throw new ServiceException(e.getErrorCode());
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " saveCustomerInfo  error ",e);
			throw new ServiceException(ReturnCode.RETURN_CODE_ERROR);
		}

	}

	@Override
	public void updateCustomerInfo(Customer customer) throws ServiceException {
		if(customer == null){
			throw new ServiceException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			Date now = Calendar.getInstance().getTime();
			customer.setUpdateTime(now);
			customer.setCreateTime(now);
			
			customerDao.update(customer);
		} catch (DaoException e) {
			throw new ServiceException(e.getErrorCode());
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " updateCustomerInfo  error ",e);
			throw new ServiceException(ReturnCode.RETURN_CODE_ERROR);
		}
		
	}

	@Override
	public void deleteCustomerInfo(Integer id) throws ServiceException {
		if(id == null){
			throw new ServiceException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			customerDao.delete(id);
			
		} catch (DaoException e) {
			throw new ServiceException(e.getErrorCode());
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " deleteCustomerInfo  error ",e);
			throw new ServiceException(ReturnCode.RETURN_CODE_ERROR);
		}
		
	}

	@Override
	public Customer getCustomerById(Integer id) throws ServiceException {
		if(id == null){
			throw new ServiceException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			return customerDao.getCustomerById(id);
			
		} catch (DaoException e) {
			throw new ServiceException(e.getErrorCode());
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " deleteCustomerInfo  error ",e);
			throw new ServiceException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public Customer getCustoemrByName(String name) throws ServiceException {
		if(name == null){
			throw new ServiceException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			return customerDao.getCustoemrByName(name);
			
		} catch (DaoException e) {
			throw new ServiceException(e.getErrorCode());
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " deleteCustomerInfo  error ",e);
			throw new ServiceException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

	@Override
	public List<Contact> getContactById(Integer customerId)
			throws ServiceException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long getDatagridTotal(Customer customer) throws ServiceException {
		if(customer == null){
			throw new ServiceException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			return customerDao.getDatagridTotal(customer);
			
		} catch (DaoException e) {
			throw new ServiceException(e.getErrorCode());
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " deleteCustomerInfo  error ",e);
			throw new ServiceException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

}
